PROJECT = "httpdemo"
VERSION = "1.0.0"

-- !字体设置：font_opposansm12_chinese
-- sys库是标配
_G.sys = require("sys")
--[[特别注意, 使用http库需要下列语句]]
_G.sysplus = require("sysplus")

---需要在云编译时添加SPI协议
spi_lcd = spi.deviceSetup(2, 7, 0, 0, 8, 40000000, spi.MSB, 1, 1) -- https://wiki.luatos.com/api/spi.html
-- spi.setup(id, cs, CPHA, CPOL, dataw, bandrate, bitdict, ms, mode)

lcd.init("st7789", {
    port = "device",
    pin_dc = 6,
    pin_pwr = 11,
    pin_rst = 10,
    direction = 0,
    w = 240,
    h = 320,
    xoffset = 0,
    yoffset = 0
}, spi_lcd)
-- 如果显示颜色相反，关闭反色
lcd.invoff()

sys.taskInit(function()

    sys.wait(1000)
    log.info(wlan.init())
    wlan.setMode(wlan.STATION)
    wlan.connect("ESP32", "776654321")

    ---等待wifi连接
    while not wlan.ready() do
        log.info("Disconnection")
        sys.wait(1000)
    end
    log.info("ready !!")

    lcd.setFont(lcd.font_opposansm12_chinese) -- 具体取值可参考api文档的常量表
    while 1 do

        ---1.http连接,code为响应代码,body是返回值
        local code_today, headers_today, body_today = http.request("GET",
                                                                   "https://api.seniverse.com/v3/weather/now.json?key=SeiP2ViJVtkngR1s5&location=luoyang&language=zh-Hans&unit=c")
                                                          .wait() -- 留意末尾的.wait()
        local code_3days, headers_3days, body_3days = http.request("GET",
                                                                   "https://api.seniverse.com/v3/weather/daily.json?key=SeiP2ViJVtkngR1s5&location=luoyang&language=zh-Hans&unit=c&start=0&days=3")
                                                          .wait() -- 留意末尾的.wait()

        ---2.因为body中有[]，无法对其进行table解析，只能先对body进行字符串操作，去除[]后在进行table解析
        -- print(body_3days)

        --=3天date和last_update参数的位置获取，用于分割body为3个table
        today_start, today_end = string.find(body_3days, "date")
        tomorrow_start, tomorrow_end =string.find(body_3days, "date", today_end)
        aftertomorrow_start, afterttomorrow_end =string.find(body_3days, "date", tomorrow_end)
        last_update_start, last_update_end =string.find(body_3days, "last_update")
        --=以上数据的打印验证
        -- print(today_start,today_end)
        -- print(tomorrow_start,tomorrow_end)
        -- print(aftertomorrow_start,afterttomorrow_end)
        -- print(last_update_start,last_update_end)
        --=打印分割后的3个table
        print(body_3days:sub(today_start-2, tomorrow_start-4))
        print(body_3days:sub(tomorrow_start-2, aftertomorrow_start-4))
        print(body_3days:sub(aftertomorrow_start-2, last_update_start-4))
        --!将3个table分别装入变量,需先将其从string转换为table
        local API_weather_today =json.decode(body_3days:sub(today_start - 2,tomorrow_start - 4))
        local API_weather_tomorrow = json.decode(body_3days:sub(tomorrow_start - 2,aftertomorrow_start - 4))
        local API_weather_aftertomorrow = json.decode(body_3days:sub(aftertomorrow_start - 2,last_update_start - 4))
        --print(API_weather_today.date)

        lcd.clear() -- !在显示前先进行清屏操作
        --=今天天气
        lcd.drawStr(15 * 2, 30 * 1, API_weather_today.date)
        lcd.drawStr(15 * 2, 30 * 2, "白天天气: " .. API_weather_today.text_day.."  夜间天气: "..API_weather_today.text_night) -- !使用汉字必须使固件中有汉字的库
        lcd.drawStr(15 * 2, 30 * 3, "最高气温: " .. API_weather_today.high.."  最低气温: "..API_weather_today.low)
        lcd.drawStr(15 * 2, 30 * 4, "风向: " .. API_weather_today.wind_direction.."  风速: "..API_weather_today.wind_speed)
        lcd.drawStr(15 * 2, 30 * 5, "湿度: " .. API_weather_today.humidity)
        sys.wait(3000)
        lcd.clear()

        --=明天天气
        lcd.drawStr(15 * 2, 30 * 1, API_weather_tomorrow.date)
        lcd.drawStr(15 * 2, 30 * 2, "白天天气: " .. API_weather_tomorrow.text_day.."  夜间天气: "..API_weather_tomorrow.text_night) -- !使用汉字必须使固件中有汉字的库
        lcd.drawStr(15 * 2, 30 * 3, "最高气温: " .. API_weather_tomorrow.high.."  最低气温: "..API_weather_tomorrow.low)
        lcd.drawStr(15 * 2, 30 * 4, "风向: " .. API_weather_tomorrow.wind_direction.."  风速: "..API_weather_tomorrow.wind_speed)
        lcd.drawStr(15 * 2, 30 * 5, "湿度: " .. API_weather_today.humidity)
        sys.wait(3000)
        lcd.clear()

        --=明天天气
        lcd.drawStr(15 * 2, 30 * 1, API_weather_aftertomorrow.date)
        lcd.drawStr(15 * 2, 30 * 2, "白天天气: " .. API_weather_aftertomorrow.text_day.."  夜间天气: "..API_weather_aftertomorrow.text_night) -- !使用汉字必须使固件中有汉字的库
        lcd.drawStr(15 * 2, 30 * 3, "最高气温: " .. API_weather_aftertomorrow.high.."  最低气温: "..API_weather_aftertomorrow.low)
        lcd.drawStr(15 * 2, 30 * 4, "风向: " .. API_weather_aftertomorrow.wind_direction.."  风速: "..API_weather_aftertomorrow.wind_speed)
        lcd.drawStr(15 * 2, 30 * 5, "湿度: " .. API_weather_aftertomorrow.humidity)
        sys.wait(3000)


    end
end)

-- 用户代码已结束---------------------------------------------
-- 结尾总是这一句
sys.run()
-- sys.run()之后后面不要加任何语句!!!!!
